package com.jh.cloud.bus.spring;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.EventListener;

public class SpringAnnotationDrivenEvent {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注册当前Bean 为了将@EventListener注册到Spring
        context.register(SpringAnnotationDrivenEvent.class);
        context.refresh();
        // 发布事件
        context.publishEvent(new MyEvent("Hello World"));
        context.close();
    }

    private static class MyEvent extends ApplicationEvent {
        public MyEvent(Object source) {
            super(source);
        }
    }

    @EventListener
    public void onMessage(MyEvent event) {
        System.err.println("监听到事件：" + event.getSource());
    }
}
